package org.lanqiao.oa.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import org.lanqiao.oa.dao.PermissionDao;
import org.lanqiao.oa.dao.UserDao;
import org.lanqiao.oa.dao.UserRoleRelationDao;
import org.lanqiao.oa.exception.LoginException;
import org.lanqiao.oa.mapper.PaginationCondition;
import org.lanqiao.oa.mapper.Role;
import org.lanqiao.oa.mapper.User;
import org.lanqiao.oa.service.UserService;
import org.lanqiao.oa.utils.DateUtils;
import org.lanqiao.oa.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleRelationDao userRoleRelationDao;
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询所有用户
     * @param page
     * @return 用户集合
     */
    @Override
    public List<User> findByPage(Page page) {
        return userDao.findByPage((page.getCurrent()-1)*page.getSize(),page.getSize());
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @Override
    public Boolean saveUser(User user) {
        user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        Integer integer = userDao.saveUser(user);
        return integer>0;
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public User selectById(String id) {
        return userDao.selectById(id);
    }

    /**
     * 根据用户id查询权限
     * @param id
     * @return
     */
    @Override
    public List<PermissionVo> getUserPermissionById(String id) {
        //查询用户所拥有的角色id
        List<String> roleIds = userRoleRelationDao.selectRoleIdListByUserId(id);
        List<PermissionVo> permissionListByRoleIds = permissionDao.getPermissionListByRoleIds(roleIds);
        System.out.println("========================");
        permissionListByRoleIds.forEach(System.out::println);
        return permissionDao.getPermissionListByRoleIds(roleIds);
    }

    /**
     * 根据id删除用户
     * @param uid
     * @return
     */
    @Override
    public Boolean deleteUserByUid(String uid) {
        Integer integer = userDao.deleteUserByUid(uid);
        return integer>0;
    }

    /**
     * 根据id修改用户
     * @param user
     * @return
     */
    @Override
    public Boolean updateUserById(User user) {
        Integer integer = userDao.updateUserById(user);
        return integer>0;
    }

    /**
     * 修改用户状态
     * @param uid
     * @param status
     * @return
     */
    @Override
    public Boolean updatelockStatus(String uid, Integer status) {
        return userDao.updatelockStatus(uid,status)>0;
    }

    /**
     * 根据条件查询用户
     * @param condition
     * @return
     */
    @Override
    public PaginationCondition<User> selectUserByCondition(PaginationCondition condition) {
        int index = condition.getIndex();
        condition.setIndex((index-1)*condition.getSize());//设置查询的分页
        condition.setList(userDao.selectUserByCondition(condition));//设置查询的数据
        Integer usersCount = userDao.selectUserByConditionCount(condition);//获取总条数

        condition.setIndex(index);//设置当前页
        condition.setTotalNumber(usersCount);//设置总条数

        condition.setPageCount(
                usersCount%condition.getSize()==0?
                        usersCount/condition.getSize():
                        usersCount/condition.getSize()+1);
        return condition;
    }

    /**
     * 根据id获取用户的角色
     * @param id
     * @return
     */
    @Override
    public Map<String, List<Role>> getAssignRole(String id) {
        List<Role> assignRoleByUserId = userRoleRelationDao.getAssignRoleByUserId(id);
        List<Role> unAssignRoleByUserId = userRoleRelationDao.getUnAssignRoleByUserId(id);
        Map<String, List<Role>> map = new HashMap<>();
        map.put("assignRole",assignRoleByUserId);
        map.put("unAssignRole",unAssignRoleByUserId);
        return map;
    }

    /**
     * 给用户分配权限
     * @param uid
     * @param rid
     * @return
     */
    @Override
    public Boolean assignRole(String uid, String rid) {
        return userRoleRelationDao.assignRole(uid,rid)>0;
    }

    /**
     * 删除用户的权限
     * @param uid
     * @param rid
     * @return
     */
    @Override
    public Boolean unAssignRole(String uid, String rid) {
        return userRoleRelationDao.unAssignRole(uid,rid)>0;
    }

    /**
     * 用户登录
     * @param user
     * @param ip
     * @return
     */
    @Override
    public User login(User user, String ip) throws RuntimeException {

        User login = userDao.login(user);
        if(login==null){
            throw new LoginException("用户名或者密码错误！！");
        }
        //判断ip地址是否合法
        String uips = login.getAllowIps();
        if(uips!=null && uips!="" && !uips.contains(ip)){
            throw new LoginException("登录ip不合法！！");
        }
        //判断账号是否失效
        String expireTime = login.getExpireTime();
        if(expireTime!=null && expireTime!="" && DateUtils.isDate(expireTime)){
            throw new LoginException("账号已经过期！！");
        }

        Integer lockStatus = login.getLockStatus();
        if(lockStatus!=null && lockStatus!=1){
            throw new LoginException("账号已经锁定！！");
        }
        return login;
    }
}
